#coding utf-8
import re
str1 = r'Track Every Coin, a cool expense tracker http://trackeverycoin.com,https://baidu.com @5RK @txt 9'
#iter1 = re.finditer(r'(?<=@)\w+|http://[\w\~\#\:\.\?\+\=\&\%\@\-\\\/]+|https://[\w\~\#\:\.\?\+\=\&\%\@\-\\\/]+',str1)# 2011 4 11

gwt_url = 'http://www.google.com/gwt/x?u='
twitter_url = 'http://twitter.com/'

def get_gwt_twitter_url(username):
    return gwt_url + twitter_url + username
    
def get_url_text_at_mixed_list(str_src):
    iter1 = re.finditer(r'@\w+'
    '|http://[\w\~\#\:\.\?\+\=\&\%\@\-\\\/]+'
    '|https://[\w\~\#\:\.\?\+\=\&\%\@\-\\\/]+'
    ,str_src)# 2011 4 11
    #print str_src
    list1 = []
    pos = 0
    for each in iter1 : 
        str2 = str_src[pos : each.start()]
        if len(str2):
            t1 = str2,None
            list1.append(t1)
        if each.group().startswith('@'):
            t1 = each.group(),get_gwt_twitter_url(each.group().replace('@',''))#replace @ with whitespace
        else:
            '''
            goo_gl_url = googl.Get_Goole_Short_Link(gwt_url + each.group())
            if goo_gl_url:
                t1 = each.group(),goo_gl_url
            else:
                t1 = each.group(),gwt_url + each.group()
            '''
            t1 = each.group(),gwt_url + each.group()
        list1.append(t1)
        pos = each.end() 
    if pos < len(str_src):#there still left text
        #print pos,len(str_src)
        t1 = str_src[pos:len(str_src)],None
        list1.append(t1)
    if not len(list1):#totally text,no link,no at
        t1 = str_src,None
        list1.append(t1)
    #for text1,url1 in list1:
    #    print text1,url1
    return list1

#get_url_text_at_mixed_list(str1)
    
    
